package com.clwl.supervise.superviseapp.util.bigdata;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * http 工具类
 */
public class HttpUtil {

	
	public static String get(String requestUrl, String accessToken,String tokenUser,String tokenCode) {
		String result = "";
		try {
			URL url = new URL(requestUrl);
	        // 打开和URL之间的连接
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setRequestMethod("GET");
	        // 设置通用的请求属性
	        connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
	        connection.setRequestProperty("Connection", "Keep-Alive");
	        if(tokenUser != null && !tokenUser.equals("")) {
	        	connection.setRequestProperty("Token-User", tokenUser);
	        }
	        if(tokenCode != null && !tokenCode.equals("")) {
	        	connection.setRequestProperty("Token-Code", tokenCode);
	        }
	        connection.setUseCaches(false);
	        connection.setDoOutput(true);
	        connection.setDoInput(true);

	        // 建立实际的连接
	        connection.connect();
	        // 获取所有响应头字段
	        Map<String, List<String>> headers = connection.getHeaderFields();
	        // 遍历所有的响应头字段
	        for (String key : headers.keySet()) {
	            System.err.println(key + "--->" + headers.get(key));
	        }
	        // 定义 BufferedReader输入流来读取URL的响应
	        BufferedReader in = null;
	        in = new BufferedReader(
	                new InputStreamReader(connection.getInputStream(), "UTF-8"));
	        
	        String getLine;
	        while ((getLine = in.readLine()) != null) {
	            result += getLine;
	        }
	        in.close();
	        System.err.println("result:" + result);
		}catch (Exception e) {
			// TODO: handle exception
		e.printStackTrace();
		}
		
        return result;
    }
	




    public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding,String tokenUser,String tokenCode)
            throws Exception {
        URL url = new URL(generalUrl);
        // 打开和URL之间的连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        // 设置通用的请求属性
        connection.setRequestProperty("Content-Type", contentType);
        connection.setRequestProperty("Connection", "Keep-Alive");
        if(tokenUser != null && !tokenUser.equals("")) {
        	connection.setRequestProperty("Token-User", tokenUser);
        }
        if(tokenCode != null && !tokenCode.equals("")) {
        	connection.setRequestProperty("Token-Code", tokenCode);
        }
        
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);

        // 得到请求的输出流对象
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(params.getBytes(encoding));
        out.flush();
        out.close();

        // 建立实际的连接
        connection.connect();
        // 获取所有响应头字段
        Map<String, List<String>> headers = connection.getHeaderFields();
        // 遍历所有的响应头字段
        for (String key : headers.keySet()) {
            System.err.println(key + "--->" + headers.get(key));
        }
        // 定义 BufferedReader输入流来读取URL的响应
        BufferedReader in = null;
        in = new BufferedReader(
                new InputStreamReader(connection.getInputStream(), encoding));
        String result = "";
        String getLine;
        while ((getLine = in.readLine()) != null) {
            result += getLine;
        }
        in.close();
        System.err.println("result:" + result);
        return result;
    }
    
	/**
	 * Unicode字符串转中文
	 * @param str
	 * @return
	 */
	public static String unicodeToString(String str) {
	    Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
	    Matcher matcher = pattern.matcher(str);
	    char ch;
	    while (matcher.find()) {
	        ch = (char) Integer.parseInt(matcher.group(2), 16);
	        str = str.replace(matcher.group(1), ch + "");
	    }
	    return str;
	}
}

